package com.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.core.service.impl.CoreServiceImpl;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.dao.SkuMapper;
import com.changgou.goods.dao.SpuMapper;
import com.changgou.goods.pojo.*;
import com.changgou.goods.service.SpuService;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.channels.SelectionKey;
import java.util.Date;
import java.util.List;
import java.util.Map;

/****
 * @Author:admin
 * @Description:Spu业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class SpuServiceImpl extends CoreServiceImpl<Spu> implements SpuService {

    @Autowired
    IdWorker idWorker;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    private SpuMapper spuMapper;

    @Autowired
    public SpuServiceImpl(SpuMapper spuMapper) {
        super(spuMapper, Spu.class);
        this.spuMapper = spuMapper;
    }

    /**
     * 保存商品信息
     * @param goods
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveGoods(Goods goods) {
        //从页面传过来的数据当中取出spu
        Spu spu = goods.getSpu();
        if (spu.getId() != null){
            //有值跟新
            spuMapper.updateByPrimaryKeySelective(spu);
            Sku t = new Sku();
            t.setSpuId(spu.getId());
            skuMapper.delete(t);
        }else {
            //没有值添加
            //使用雪花算法工具类生成一个主键
            //将生成的主键存入到spu当中
            spu.setId(idWorker.nextId());
            //调用dao传入spu
            spuMapper.insertSelective(spu);
        }
        addGoods(goods, spu);

    }

    private void addGoods(Goods goods, Spu spu) {

        //取出sku并遍历
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku : skuList) {
            //设置sku的主键
            sku.setId(idWorker.nextId());
            //设置sku的名字 spu名+空格—— 规格的具体的值 拼接而成的
            String name = spu.getName();
            Map<String,String> map = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
                String value = stringStringEntry.getValue();
                name+= " " + value;
            }
            sku.setName(name);

            //设置创建时间
            sku.setCreateTime(new Date());
            sku.setUpdateTime(sku.getCreateTime());
            //设置spuid
            sku.setSpuId(spu.getId());
            //根据id去查询名字
            Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id());
            //设置商品的三级分类id
            //设置商品分类的名称
            if (category!= null) {
                sku.setCategoryId(spu.getCategory3Id());
                sku.setCategoryName(category.getName());
            }
            //根据spu中brand的id去查询brand的name
            Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
            if (brand != null){
                sku.setBrandName(brand.getName());
            }
            skuMapper.insertSelective(sku);
        }
    }

    @Override
    public Goods goodsById(Long id) {
        Goods goods = new Goods();
        Spu t1 = new Spu();
        t1.setId(id);
        t1.setIsDelete("0");
        List<Spu> spuList = spuMapper.select(t1);
        for (Spu spu : spuList) {
            goods.setSpu(spu);
        }

        //相当于 where
        Sku t = new Sku();
        //相当于 spu_id = ?
        t.setSpuId(id);
        //相当于select * from tb_sku
        List<Sku> sku = skuMapper.select(t);


        goods.setSkuList(sku);
        return goods;

    }
}
